//
//  Dictionary+AG.swift
//  BWExtensionKit
//
//  Created by zhuxuhong on 2022/3/2.
//

import Foundation

extension Dictionary {
    public var bw_urlParamsJoined: String {
        var url = enumerated().map{ _,item in
            "\(item.key)=\(item.value)"
        }.joined(separator: "&")
        url = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? url
        return url
    }
    
    public func bw_jsonData(prettify: Bool = false) -> Data? {
        guard JSONSerialization.isValidJSONObject(self) else {
            return nil
        }
        let options = (prettify == true) ? JSONSerialization.WritingOptions.prettyPrinted : JSONSerialization.WritingOptions()
        return try? JSONSerialization.data(withJSONObject: self, options: options)
    }
}
